// miniprogram/pages/my/index.js
const {
  wxLogin,
  wxMobileLogin,
  decodeUserInfo,
  sysSetting
} = require('../../api/login')
const config = require('../../config/config')
const {
  checkLogin
} = require('../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navHeight: getApp().globalData.navHeight,
    navTop: getApp().globalData.navTop,
    menuHeight: getApp().globalData.menuHeight,
    height: 0,
    isLogin: false,
    userInfo: {},
    isVip: false,
    newNavList: [
    //   {
    //   name: '我的运单',
    //   url: '/pages/users/myorder/index',
    //   icon: '/images/user/deparute_icon.png',
    //   width: 32,
    //   height: 32,
    //   isVip: false,
    // },
    // {
    //   name: '我的车辆',
    //   url: '/pages/users/myvehicle/index',
    //   icon: '/images/user/icon_car.png',
    //   width: 32,
    //   height: 32,
    //   isVip: false,
    // },
    {
        name: '个人资料',
        url: '/pages/users/index/index',
        icon: '/images/user/user_icon1.png',
        width: 32,
        height: 32,
        isVip: false,
      },
      {
        name: '关于我们',
        url: '/pages/users/about/index',
        icon: '/images/user/user_icon2.png',
        width: 32,
        height: 32,
        isVip: false,
      },
      {
        name: '意见反馈',
        url: '/pages/users/feedback/index',
        icon: '/images/user/user_icon3.png',
        width: 32,
        height: 34,
        isVip: false,
      },
    ],
    phoneNumber: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideTabBar()
    getApp().editTabbar()
    this.setData({
      height: wx.getSystemInfo().windowHeight,
      userInfo: wx.getStorageSync('userInfo')
    })
    if (wx.getStorageSync('token')) {
      this.setData({
        isLogin: true
      })
    }
  },
  score(e) {
    wx.navigateTo({
      url: '/pages/users/score/index',
    })
  },
  collect(e) {
    wx.navigateTo({
      url: '/pages/users/volunteer/index',
    })
  },

  logout() {
    var that = this;
    wx.showLoading({
      title: '正在退出',
    })
    wx.clearStorageSync()
    setTimeout(() => {
      wx.hideLoading()
      wx.switchTab({
        url: '/pages/index/index',
      })
      that.setData({
        isVip: false,
      })
    }, 1000);
  },
  onShow: function () {
    console.log('12')
    checkLogin('none').then((res) => {
      this.setData({
        isLogin: true,
      })
      this.fetchUserInfo()
    }).catch((e) => {
      this.setData({
        isLogin: false,
      })
    })
  },
  goto(e) {
    const url = e.currentTarget.dataset.url
    checkLogin().then((res) => {
      wx.navigateTo({
        url: url,
      })
    })
  },

  async getConfig() {
    const {
      result
    } = await sysSetting()
    this.setData({
      phoneNumber: result[0].mobile
    })
  },
  login() {
    wx.navigateTo({
      url: '/pages/login/index',
    })
  },
  async fetchUserInfo() {
    const
      result = await decodeUserInfo()
    this.setData({
      userInfo: result,
    })

  },
  scrollTop(e) {
    console.log(e)
  },

  getUserProfile(e) {
    var that = this
    let newTime = Math.round(new Date())
    wx.showLoading({
      title: '登陆中',
    })
    wx.setStorageSync(config.storageKey.loginTime, newTime);
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: async function (res) {
        console.log(res)
        const {
          encryptedData,
          iv,
        } = res;
        const info = res.userInfo
        wx.login({
          success: async function (ress) {
            const param = {
              avatarUrl: info.avatarUrl,
              city: info.city,
              code: ress.code,
              country: info.city,
              encryptedData,
              iv,
              language: info.language,
              merchantId: 0,
              nickName: info.nickName,
              promoterId: 0,
              province: info.province,
              sex: info.gender,
              promoteUser: wx.getStorageSync('promoteUser'),
            }
            wx.hideLoading()
            const {
              result
            } = await wxLogin(param);
            const {
              userInfo,
              token,
            } = result
            wx.setStorageSync(config.storageKey.userId, userInfo.id);
            wx.setStorageSync(config.storageKey.token, token)
            that.fetchUserInfo()
            that.setData({
              userInfo,
              isLogin: true,
              isVip: (userInfo.isVip && userInfo.isVip) > 0,
            })
            if (userInfo.mobile == '') {
              return
            }
            wx.setStorageSync(config.storageKey.mobile, userInfo.mobile)

          }
        })

      },
      fail: function (res) {
        wx.hideLoading()
      }
    })

  },
  async getPhoneNumber(e) {
    const {
      encryptedData,
      iv
    } = e.detail
    wx.login({
      success: async function (res) {
        console.log(res.code)
        var code = res.code
        const param = {
          code,
          encryptedData,
          iv
        }
        const data = await wxMobileLogin(param)
      }
    })
  },
})